package com.qm.console.component.util;
import com.alibaba.fastjson.JSONArray;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class CommonUtil {


    public static String[] setStartTime(String startTime, String endTime)
    {
        DateUtil dateUtil = new DateUtil();
        String[] stringTime = new String[2];
        if (null == startTime && null == endTime) {
            startTime = dateUtil.getCurrentTime("yyyy-MM-dd") + " 00:00:00";
            endTime = dateUtil.getCurrentTime("yyyy-MM-dd") + " 23:59:59";
        } else if (null == startTime && null != endTime) {
            //默认填一个
            startTime = "2018-09-01 00:00:00";
        } else if (null != startTime && null == endTime) {
            endTime = dateUtil.getCurrentTime("yyyy-MM-dd") + " 23:59:59";
        }
        if(startTime.length() <=10)
        {
            startTime = startTime +  " 00:00:00";
        }
        if (endTime.length() <=10)
        {
            endTime = endTime +  " 23:59:59";
        }
        stringTime[0] = startTime;
        stringTime[1] = endTime;
        return stringTime;
    }

    public static List<String> getTableListFromDate(String startTime, String endTime,String baseTable)
    {
        List<String> tableList = new ArrayList<>();
        String[] startArray = startTime.split("-");
        String[] endArray = endTime.split("-");
        int startMonth = Integer.parseInt(startArray[1]) + Integer.parseInt(startArray[0]) * 12;
        int endMonth = Integer.parseInt(endArray[1]) + Integer.parseInt(endArray[0]) * 12;
        for (int i = startMonth; i <= endMonth; i++) {
            int m =i%12==0?12:i%12;
            int y = i%12==0?i/12-1:i/12;
            tableList.add(baseTable + "_" + String.format("%02d", y) + "_" + String.format("%02d", m));
        }
        return tableList;
    }

    public static String join(String join,List<String> list){
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<list.size();i++){
            if(i==(list.size()-1)){
                sb.append(list.get(i));
            }else{
                sb.append(list.get(i)).append(join);
            }
        }
        return new String(sb);
    }


    public static StringBuffer camelToUnderline(StringBuffer str) {
        Pattern pattern = Pattern.compile("[A-Z]");
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer(str);
        if(matcher.find()) {
            sb = new StringBuffer();
            //将当前匹配子串替换为指定字符串，并且将替换后的子串以及其之前到上次匹配子串之后的字符串段添加到一个StringBuffer对象里。
            //正则之前的字符和被替换的字符
            matcher.appendReplacement(sb,"_"+matcher.group(0).toLowerCase());
            //把之后的也添加到StringBuffer对象里
            matcher.appendTail(sb);
        }else {
            return sb;
        }
        return camelToUnderline(sb);
    }

    public static JSONArray pageLimit(JSONArray data, Integer offset, Integer limit){
        if(data.size()<=limit)
            return data;
        else
        {
            JSONArray array = new JSONArray();
            for(int i = offset;i<offset+limit&&i<data.size();i++){
                array.add(data.get(i));
            }
            return array;
        }
    }

    public static StringBuffer underlineToCamel(StringBuffer str) {
        //利用正则删除下划线，把下划线后一位改成大写
        Pattern pattern = Pattern.compile("_(\\w)");
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer(str);
        if(matcher.find()) {
            sb = new StringBuffer();
            //将当前匹配子串替换为指定字符串，并且将替换后的子串以及其之前到上次匹配子串之后的字符串段添加到一个StringBuffer对象里。
            //正则之前的字符和被替换的字符
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
            //把之后的也添加到StringBuffer对象里
            matcher.appendTail(sb);
        }else {
            return sb;
        }
        return underlineToCamel(sb);
    }

    public static void getDownload(HttpServletResponse response, String filePath) throws IOException {
        OutputStream os = response.getOutputStream();
        FileInputStream fis = new FileInputStream(filePath);
        byte[] buffer = new byte[1024];
        BufferedInputStream bis = new BufferedInputStream(fis);
        int i = bis.read(buffer);
        while(i != -1){
            os.write(buffer);
            i = bis.read(buffer);
        }
        bis.close();
        os.close();
    }
}
